import pygame.font

class ScoreBoard:
    ''' 玩家得分板 '''
    def __init__(self, game):
        self.game = game  # 存储游戏对象的引用
        self.screen = game.screen  # 获取游戏屏幕
        self.screen_rect = game.screen.get_rect()  # 获取屏幕的矩形区域
        self.settings = game.settings  # 获取游戏设置

        self.text_color = (255, 255, 255)  # 文本颜色为白色
        self.font = pygame.font.SysFont(None, 48)  # 使用大小为48的系统字体

        self.prep_score_player_left()  # 准备左侧玩家的分数显示
        self.prep_score_player_right()  # 准备右侧玩家的分数显示

    def prep_score_player_left(self):
        ''' 准备左侧玩家的分数显示 '''
        player_left_score_str = str(self.settings.player_left_score)  # 将左侧玩家分数转换为字符串
        # 使用设置中的背景颜色渲染左侧玩家分数图像，文本颜色为白色
        self.score_image_player_left = self.font.render(
            player_left_score_str, True, self.text_color, self.settings.bg_color)
        self.score_rect_player_left = self.score_image_player_left.get_rect()
        # 将左侧玩家分数图像放置在屏幕的右侧，并稍微向左偏移
        self.score_rect_player_left.right = (self.settings.screen_width // 2) - 30

    def prep_score_player_right(self):
        ''' 准备右侧玩家的分数显示 '''
        player_right_score_str = str(self.settings.player_right_score)  # 将右侧玩家分数转换为字符串
        # 使用设置中的背景颜色渲染右侧玩家分数图像，文本颜色为白色
        self.score_image_player_right = self.font.render(
            player_right_score_str, True, self.text_color, self.settings.bg_color)
        self.score_rect_player_right = self.score_image_player_right.get_rect()
        # 将右侧玩家分数图像放置在屏幕的左侧，并稍微向右偏移
        self.score_rect_player_right.left = (self.settings.screen_width // 2) + 30

    def show_score(self):
        ''' 在屏幕上显示分数 '''
        # 绘制左侧玩家分数图像到其指定的矩形位置上
        self.screen.blit(self.score_image_player_left, self.score_rect_player_left)
        # 绘制右侧玩家分数图像到其指定的矩形位置上
        self.screen.blit(self.score_image_player_right, self.score_rect_player_right)
